<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Frontend_helper
{
    private $CI;

    public function __construct()
    {
        $this->CI = & get_instance();
    }

    public function get_resolutions()
    {
        $data = array();
        $this->CI->db->cache_set_path(dirname($this->CI->db->cachedir) . '/frontend_common/');
        $this->CI->db->custom_seqgment('resolution_sidebar', 'index');
        $this->CI->load->model('resolution_model');
        if ($this->CI->config->item('display_only_available_resolutions')) {
            $data['resolutions'] = $this->resolutions_grouping($this->CI->resolution_model->get_resolutionsWithCount(true));
        } else {
            $data['resolutions'] = $this->resolutions_grouping($this->CI->resolution_model->get_resolutionsWithCount());
        }
        $this->CI->db->reset_custom_seqgment();
        return $this->CI->tpl->fetch('sidebar/resolutions_list', $data);
    }

    public function resolutions_grouping($data)
    {
        if (empty($data))
            return false;
        $return = array();
        foreach ($data as $d) {
            $return[$d->resolution_group_name][] = $d;
        }
        return $return;
    }

    public function get_categories()
    {
        $data = array();
        $this->CI->db->cache_set_path(dirname($this->CI->db->cachedir) . '/frontend_common/');
        $this->CI->db->custom_seqgment('categories_sidebar', 'index');
        $this->CI->load->model('category_model');

        $data['categories'] = $this->CI->category_model->get_CategoryTree(array(
            'closure' => true,
            'display_only_available_category' => $this->CI->config->item('display_only_available_category')
        ));


        $this->CI->db->reset_custom_seqgment();
        return $this->CI->tpl->fetch('sidebar/categories_list', $data);
    }

    public function get_query_string($exclude = array(), $append = array())
    {
        $allowed = array('page', 'sort', 'color', 'time','category');
        $found_parameters = array();
        foreach ($allowed as $al) {
            if (isset($_GET[$al]) && !in_array($al, $exclude))
                $found_parameters[$al] = $_GET[$al];
        }
        if (!empty($append))
            $found_parameters = $found_parameters + $append;
        return (!empty($found_parameters) ? '?'.http_build_query($found_parameters) : "");
    }

    public function get_query_string_active($parameter, $value, $class_name = 'active')
    {
        return isset($_GET[$parameter]) && strtolower($_GET[$parameter]) == strtolower($value) ? $class_name : '';
    }
}